package com.my.chapter15;


import net.mindview.util.Generator;

import java.util.Iterator;


public class practice7 {
    public static void main(String[] args) {
        for(int i:new IterableFibonacci(6)){
            System.out.print(i + " ");
        }
        System.out.println("");
    }
}

class Fibonacci implements Generator<Integer> {
    private int count = 0;
    public Integer next(){
        return fib(count++);
    }
    private int fib(int n){
        if(n<2) return 1;
        return fib(n-2) + fib(n-1);
    }
}

class IterableFibonacci implements Iterable<Integer> {
    private Fibonacci fib = new Fibonacci();
    private int n;
    public IterableFibonacci(int count){
        n = count;
    }

    @Override
    public Iterator<Integer> iterator() {
        return new Iterator<Integer>() {
            @Override
            public boolean hasNext() {
                return n>0;
            }

            @Override
            public Integer next() {
                n--;
                return fib.next();
            }
            public void remove() { // Not implemented
                throw new UnsupportedOperationException();
            }
        };
    }
}

